import { axios } from '@/utils/request'
import { mimeMap, resolveBlob } from '@/utils/download'
import { buildUUID } from '@/utils/uuid'
import moment from 'moment'
import { isArray, isString } from 'lodash-es'
import { isJSON } from '@/utils/common'

export const convertFieldValue = (answerItem, item) => {
  let contentArr = []

  try {
    if (answerItem && answerItem.content && answerItem.content !== '[]') {
      contentArr = JSON.parse(answerItem.content)
    } else if (item.itemDefault) {
      contentArr = isArray(item.itemDefault) ? item.itemDefault : JSON.parse(item.itemDefault)
    } else {
      contentArr = []
    }
  } catch (error) {
    contentArr = []
  }

  let text = contentArr[0]

  if (text && text.toString()[0] === '0' && item.itemType === 'Select') {
    return text
  }

  //数组型的答案使用map转换
  if (
    (contentArr && contentArr.length > 1) ||
    item.itemType === 'CheckboxGroup' ||
    item.itemType === 'Honor' ||
    item.itemType === 'AreaPicker' ||
    item.itemType === 'AreaStreetPicker'
  ) {
    text = contentArr.map(m =>
      isString(m) && m.trim() != '' && !isNaN(m) && item.isInternalInterface != 2 ? Number(m) : m
    )
  } else {
    text =
      text && !isNaN(text) && item.isInternalInterface != 2 && text.length < 10 && item.itemType !== 'YearPicker'
        ? Number(text)
        : text === 'true'
          ? true
          : text === 'false'
            ? false
            : isString(text) && text && text.indexOf(',') != -1 && text.indexOf('https') === -1
              ? text.split(',')
              : text
  }

  // if ((item.itemType === 'UploadImg' || item.itemType === 'Attachment') && item.length > 1) text = contentArr

  try {
    if (item.itemType === 'UploadFile' || item.itemType === 'Attachment') {
      // console.log('contentArr--->', contentArr)
      text = contentArr.map(m => {
        let file = JSON.parse(m) || {}
        return {
          ...file,
          uid: buildUUID(),
          status: 'done'
        }
      })
    }
    /** 处理 学校 学院 专业 值为Object的情况 */
    if (item.itemType === 'InputSearch' && contentArr.length && isJSON(contentArr[0])) {
      text = contentArr.map(v => JSON.parse(v))
    }
  } catch (error) {
    text = contentArr
  }

  if (item.itemType === 'TimePicker') {
    text = text && moment(text, 'HH:mm')
  }

  return text
}

const root = 'system/'
const api = {
  /**
   * 职务管理
   */
  //根据职务级别查询职位信息
  postMapList: root + 'coc-leading-member-position/getMapList',
  // 查询职务
  postGetList: root + 'coc-leading-member-position/getList',
  //新增/修改
  postAddOrUpdate: root + 'coc-leading-member-position/addOrUpdate',
  //删除
  postDel: root + '/coc-leading-member-position/deleteMember',

  /**
   * 入会配置
   */
  //单位入会申请模板
  templateById: '/admin/member-apply-template/getById',
  updateApplyTemplate: '/admin/member-apply-template/updateApplyTemplate',

  getByIdApplyExplain: '/admin/member-apply-template/getByIdApplyExplain',
  updateApplyExplain: '/admin/member-apply-template/updateApplyExplain',
  customFormItemTypeList: '/admin/custom-form-item-type/list',
  customFieldLibraryList: 'admin/custom-field-library/list',
  customModuleTypeList: '/admin/custom-module-type/list',

  /**
   * 会员审核
   */
  memberApplypageList: '/admin/member-apply-form/pageList',
  memberApplyReview: '/admin/member-apply-form/review',
  paymentMember: '/admin/member-apply-form/paymentMember',
  memberApplyFormDetails: '/admin/member-apply-form/details',
  customFormStage: '/admin/custom-form-stage/getStageList', //根据表单id获取阶段列表
  memberApplyFormExportExcel: '/admin/member-apply-form/exportExcel', //会员申请导出

  memberPaymenExportExcel: '/admin/mp/member-paymenrecord/memberPaymenExportExcel', //续费审核列表导出

  memberTypeList: '/system/member/class/type/getList', //根据类型获取列表，会员职务、行业列表
  auditsChange: '/system/headquarters-member-change/review', //审核

  /**
   * 资料审核
   */
  infoApplyList: '/admin/member-info-apply/pageList', //资料审核列表
  applyInfo: '/admin/member-info-apply/getApplyInfo', //资料审核详情
  infoApplyReview: '/admin/member-info-apply/review', //资料审核

  /**
   * 会员信息
   */
  memberInfo: '/admin/custom-member-info-answer/getMemberInfo', //获取会员信息
  updateMemberInfo: '/admin/custom-member-info-answer/updateMemberInfo', //编辑会员信息

  editCurrentService: '/system/member/headquarters/addCurrentService', //新增、编辑服务专员
  addMember: '/system/member/headquarters/addMember', //新增会员
  memberQuit: '/system/member/headquarters/quit', //退会
  quitInfo: '/system/member/headquarters/getQuitInfo', //退会详情

  customExportemplate: '/system/member/headquarters/customExportemplate', //会员模板下载

  customImportExcel: '/admin/customFormInfo/asyncMemberImportExcel', //导入会员数据

  updateMemberSort: '/system/member/headquarters/updateMemberSort', //会员列表修改排序
  syncDataToCard: '/system//businessCard/cover', //同步资料到名片

  /**
   * 缴费信息
   */
  renewOfflineMemberOrderList: '/admin/mp/member-paymenrecord/renewOfflineMemberOrderList',
  paymenrecordInfo: '/admin/mp/member-paymenrecord/getById/',
  paymenrecordReview: '/admin/mp/member-paymenrecord/renew/review',

  memberApplyById: '/admin/mp/member-paymenrecord/payFees/getMemberApplyById/',
  payFeesCreatePaymenrecord: '/admin/mp/member-paymenrecord/payFees/createPaymenrecord',
  payFeesReview: '/admin//mp/member-paymenrecord/payFees/review',

  memberPaymentInfo: '/admin/mp/member-paymenrecord/getMemberPaymentInfo/',
  paymentExport: '/admin/mp/member-paymenrecord/',
  renewCreatePaymenrecord: '/admin/mp/member-paymenrecord/renew/createPaymenrecord',
  memberByIdMemberPaymenrecord: '/admin/mp/member-paymenrecord/renew/getMemberByIdMemberPaymenrecord/',

  activityCustomFormItemTypeList: '/admin/custom-form-item-type/activity/list'
}

export default api

/**
 * 根据职务级别查询职位信息
 * @export
 * @return {*}  {Promise<any>}
 */
export function postMapList(memberId) {
  return axios({
    url: api.postMapList + `?cocId=${memberId}`,
    method: 'get'
  })
}

/**
 * 查询会员单位下职务列表
 * @export
 * @param {number} memberId
 * @return {*}  {Promise<IPostGetListRes>}
 */
export function postGetList(memberId) {
  return axios({
    url: api.postGetList + `?memberId=${memberId}`,
    method: 'get'
  })
}

/**
 * 职位管理新增/修改
 * @export
 * @return {*}  {Promise<any>}
 */
export function postAddOrUpdate(data) {
  return axios({
    url: api.postAddOrUpdate,
    method: 'post',
    data
  })
}

/**
 * 职位管理删除
 * @export
 * @return {*}  {Promise<any>}
 */
export function postDel(data) {
  const { memberId, id } = data
  return axios({
    url: api.postDel + `/${memberId}/${id}`,
    method: 'delete'
  })
}

//单位入会申请模板
export function templateById(id) {
  return axios({
    url: api.templateById + `/${id}`,
    method: 'get'
  })
}

//修改入会申请模板
export function updateApplyTemplate(data) {
  return axios({
    url: api.updateApplyTemplate,
    method: 'post',
    data
  })
}

//查询自定义表单字段类型
export function customFormItemTypeList() {
  return axios({
    url: api.customFormItemTypeList,
    method: 'get'
  })
}

//查询活动自定义表单字段类型
export function activityCustomFormItemTypeList() {
  return axios({
    url: api.activityCustomFormItemTypeList,
    method: 'get'
  })
}

//字段库列表
export function customFieldLibraryList() {
  return axios({
    url: api.customFieldLibraryList,
    method: 'get'
  })
}

//字段库类型列表
export function customModuleTypeList() {
  return axios({
    url: api.customModuleTypeList,
    method: 'get'
  })
}

//查询入会须知
export function getByIdApplyExplain(id) {
  return axios({
    url: api.getByIdApplyExplain + `/${id}`,
    method: 'get'
  })
}

//修改入会须知
export function updateApplyExplain(data) {
  return axios({
    url: api.updateApplyExplain,
    method: 'post',
    data
  })
}

//入会申请列表
export function memberApplypageList(params) {
  return axios({
    url: api.memberApplypageList,
    method: 'get',
    params
  })
}

//入会审核
export function memberApplyReview(data) {
  return axios({
    url: api.memberApplyReview,
    method: 'post',
    data
  })
}

//入会缴费
export function paymentMember(data) {
  return axios({
    url: api.paymentMember,
    method: 'post',
    data
  })
}

//入会申请详情
export function memberApplyFormDetails(id) {
  return axios({
    url: api.memberApplyFormDetails + `/${id}`,
    method: 'get'
  })
}

//根据表单id获取阶段列表
export function customFormStage(id) {
  return axios({
    url: api.customFormStage + `/${id}`,
    method: 'get'
  })
}

//导出审核列表
export function memberApplyFormExportExcel(params) {
  axios({
    url: api.memberApplyFormExportExcel,
    method: 'get',
    responseType: 'blob',
    timeout: 0,
    params
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx)
  })
}

//根据类型获取列表
export function memberTypeList(type, params) {
  return axios({
    url: api.memberTypeList + `/${type}`,
    method: 'get',
    params
  })
}

/**
 * 审核
 */
export function auditsChange(data) {
  return axios({
    url: api.auditsChange + '/' + data.id + '/' + data.status,
    method: 'post',
    data
  })
}

/**
 * 资料审核
 */
//资料审核列表
export function infoApplyList(params) {
  return axios({
    url: api.infoApplyList,
    method: 'get',
    params
  })
}

//资料审核详情
export function applyInfo(id) {
  return axios({
    url: api.applyInfo + `/${id}`,
    method: 'get'
  })
}

//资料审核
export function infoApplyReview(data) {
  return axios({
    url: api.infoApplyReview,
    method: 'post',
    data
  })
}

//获取会员信息
export function memberInfo(id) {
  return axios({
    url: api.memberInfo + `/${id}`,
    method: 'get'
  })
}

//编辑会员信息
export function updateMemberInfo(data) {
  return axios({
    url: api.updateMemberInfo,
    method: 'post',
    data
  })
}

//新增、编辑服务专员
export function editCurrentService(data) {
  return axios({
    url: api.editCurrentService,
    method: 'post',
    data
  })
}

//新增会员
export function addMember(data) {
  return axios({
    url: api.addMember,
    method: 'post',
    data
  })
}

//退会
export function memberQuit(data) {
  return axios({
    url: api.memberQuit,
    method: 'post',
    data
  })
}

//退会详情
export function quitInfo(id) {
  return axios({
    url: api.quitInfo + `/${id}`,
    method: 'get'
  })
}

//会员模板下载
export function customExportemplate(params) {
  return axios({
    url: api.customExportemplate,
    method: 'get',
    responseType: 'blob',
    timeout: 0,
    params
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx)
  })
}

//导入会员数据
export function customImportExcel(data) {
  return axios({
    url: api.customImportExcel,
    method: 'post',
    data
  })
}

//更改会员排序
export function updateMemberSort(data) {
  return axios({
    url: api.updateMemberSort,
    method: 'post',
    data
  })
}

//同步入会资料到名片
export function SyncDataToCard(params) {
  return axios({
    url: api.syncDataToCard,
    method: 'get',
    params
  })
}

//续费审核列表
export function RenewOfflineMemberOrderList(params) {
  return axios({
    url: api.renewOfflineMemberOrderList,
    method: 'get',
    params
  })
}

//续费详情
export function PaymenrecordInfo(id) {
  return axios({
    url: api.paymenrecordInfo + id,
    method: 'get'
  })
}

//续费审核
export function PaymenrecordReview(data) {
  return axios({
    url: api.paymenrecordReview,
    method: 'post',
    data
  })
}

//导出续费审核列表
export function MemberPaymenExportExcel() {
  axios({
    url: api.memberPaymenExportExcel,
    method: 'get',
    responseType: 'blob',
    timeout: 0
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx)
  })
}

//根据申请ID获取缴费订单详情
export function MemberApplyById(id) {
  return axios({
    url: api.memberApplyById + id,
    method: 'get'
  })
}

//创建会费支付记录
export function PayFeesCreatePaymenrecord(data) {
  return axios({
    url: api.payFeesCreatePaymenrecord,
    method: 'post',
    data
  })
}

//缴费审核
export function PayFeesReview(data) {
  return axios({
    url: api.payFeesReview,
    method: 'post',
    data
  })
}

//获取缴费函信息
export function MemberPaymentInfo() {
  return axios({
    url: api.memberPaymentInfo,
    method: 'get'
  })
}

//查看缴费函
export function PaymentExport(data, path) {
  axios({
    url: api.paymentExport + path,
    method: 'post',
    responseType: 'blob',
    timeout: 0,
    data
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx)
  })
}

//创建会费订单
export function RenewCreatePaymenrecord(data) {
  return axios({
    url: api.renewCreatePaymenrecord,
    method: 'post',
    data
  })
}

//获取续费信息
export function MemberByIdMemberPaymenrecord(id) {
  return axios({
    url: api.memberByIdMemberPaymenrecord + id,
    method: 'get'
  })
}

//获取会员详细信息
export function MemberSettingInfo(id) {
  return axios({
    url: `/system/member/headquarters/app/sys/getByIdSettingInfo/${id}`,
    method: 'get'
  })
}

//修改会员信息
export function UpdateByIdSettingInfo(data) {
  return axios({
    url: `/system/member/headquarters/app/sys/updateByIdSettingInfo`,
    method: 'post',
    data
  })
}

//修改会员信息批量
export function batchUpdateByIdSettingInfo(data) {
  return axios({
    url: `/system/member/headquarters/batch/updateByIdSettingInfo`,
    method: 'post',
    data
  })
}

//会员附件列表
export function accessoryPageList(params) {
  return axios({
    url: `/admin/headquarters-member-accessory/pageList`,
    method: 'get',
    params
  })
}

//会员附件新增
export function accessoryAdd(data) {
  return axios({
    url: `/admin/headquarters-member-accessory/add`,
    method: 'post',
    data
  })
}

//会员附件删除
export function accessoryDel(id) {
  return axios({
    url: `/admin/headquarters-member-accessory/del/${id}`,
    method: 'post'
  })
}

//全选会员列表
export function checkedAllMember(params) {
  return axios({
    url: `/system/member/headquarters/allIds`,
    method: 'post',
    data: params
  })
}

// 导出会员列表
export function syncMemberIdcustomExportExcel(params) {
  return axios({
    url: `/admin/member/headquarters/syncMemberIdcustomExportExcel`,
    method: 'post',
    data: params
  })
}
